﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MAS_Districts : System.Web.UI.Page
    {
        private IMasDistrictBLL disBLL = new MasDistrictBLL();
        private IMasAmphurBLL ampBLL = new MasAmphurBLL();
        private IMasProvinceBLL proBLL = new MasProvinceBLL();
        private IMasRegionBLL regBLL = new MasRegionBLL();
        private ITreeViewBLL treeBLL = new TreeViewBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                RadTreeNode selected_node = RadTreeView1.SelectedNode;

                RadGrid1.Visible = false;
                lblHeader.Text = "ข้อมูลตำบล/อำเภอ/จังหวัด/ภาค";
            }

        }
        public void LoadDataGridRegions()
        {
            IList<MasRegionInfo> Info = regBLL.ListMasRegions();
            RadGrid1.DataSource = Info;

            RadGrid1.Columns[3].HeaderText = "ชื่อภาค";
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/MAS_Regions.ascx";
            RadGrid1.DataBind();
        }
        public void LoadDataGridProvinces(string Region)
        {
            IList<MasProvinceInfo> Info = proBLL.ListMasProvinces("", Region);
            RadGrid1.DataSource = Info;

            RadGrid1.Columns[3].HeaderText = "ชื่อจังหวัด";
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/MAS_Provinces.ascx";
            RadGrid1.DataBind();
        }
        public void LoadDataGridAmphurs(string Province)
        {
            IList<MasAmphurInfo> Info = ampBLL.ListAmphurs("", Province);
            RadGrid1.DataSource = Info;

            RadGrid1.Columns[3].HeaderText = "ชื่ออำเภอ";
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/MAS_Amphurs.ascx";
            RadGrid1.DataBind();
        }
        public void LoadDataGridDistricts(string Amphur)
        {
            IList<MasDistrictInfo> Info = disBLL.ListDistricts("", Amphur, "");
            RadGrid1.DataSource = Info;

            RadGrid1.Columns[3].HeaderText = "ชื่อตำบล";
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/MAS_Districts.ascx";
            RadGrid1.DataBind();
        }
        private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadDistrictTreeView("");

			foreach (TreeViewInfo info in infoList)
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }

        public void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();

            foreach (TreeViewInfo info in treeBLL.LoadDistrictTreeView(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            foreach (TreeViewInfo info in treeBLL.LoadDistrictTreeView(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Nodes.Clear();
            e.Node.ExpandMode = expandMode;
        }
        public void RefreshTree()
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;
            if (selected_node == null)
            {
                selected_node = RadTreeView1.FindNodeByValue("0");
            }
            RadTreeNodeEventArgs node = new RadTreeNodeEventArgs(selected_node);
            RadAjaxManager1.ResponseScripts.Add(@"CollapseSelectTree();");
            PopulateNodeOnEditGrid(node, TreeNodeExpandMode.ServerSideCallBack);
            RadAjaxManager1.ResponseScripts.Add(@"ExpandSelectTree();");
        }
        protected void btnExpand_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadDistrictTreeView("");

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }
        protected void btnCollapse_Click(object sender, EventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }
        protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadDistrictTreeView("");

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }

        protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);

        }
        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (RadTreeView1.SelectedNode.Level == 0)
            {
                if (e.CommandName == "Delete" && RadTreeView1.SelectedNode.Level == 0)
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);

                    regBLL.Delete(seq, 1);
                }
                LoadDataGridRegions();
            }
            else if (RadTreeView1.SelectedNode.Level == 1)
            {
                if (e.CommandName == "Delete" && RadTreeView1.SelectedNode.Level == 1)
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                    proBLL.Delete(seq, 1);
                }
                LoadDataGridProvinces(Session["Parent"].ToString().Trim());
            }
            else if (RadTreeView1.SelectedNode.Level == 2)
            {
                if (e.CommandName == "Delete" && RadTreeView1.SelectedNode.Level == 2)
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                    ampBLL.Delete(seq, 1);
                }
                LoadDataGridAmphurs(Session["Parent"].ToString().Trim());
            }
            else if (RadTreeView1.SelectedNode.Level == 3)
            {
                if (e.CommandName == "Delete" && RadTreeView1.SelectedNode.Level == 3)
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                    disBLL.Delete(seq, 1);
                }
                LoadDataGridDistricts(Session["Parent"].ToString().Trim());
            }
            RefreshTree();
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;

            string value = selected_node.Value.Trim();
            string name = selected_node.Text.Trim();
            string[] root_value = selected_node.Value.Split(new string[] { "-" }, StringSplitOptions.None);

            if (!string.IsNullOrEmpty(value))
            {
                RadGrid1.Visible = true;
                if (root_value.Length > 1)
                    Session["Parent"] = root_value[1];

                if (selected_node.Level == 0)
                {
                    LoadDataGridRegions();
                    lblHeader.Text = "ข้อมูลภาค";
                }
                else if (selected_node.Level == 1)
                {
                    LoadDataGridProvinces(root_value[1]);
                    lblHeader.Text = "ข้อมูลจังหวัดในภาค" + name;
                }
                else if (selected_node.Level == 2)
                {
                    LoadDataGridAmphurs(root_value[1]);
                    lblHeader.Text = "ข้อมูลอำเภอในจังหวัด" + name;
                }
                else if (selected_node.Level == 3)
                {
                    LoadDataGridDistricts(root_value[1]);
                    lblHeader.Text = "ข้อมูลตำบลในอำเภอ" + name;
                }
            }
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {

        }
    }
}
